// 清理脚本 by略游
// 所有客户端都需要执行，这样才有函数的定义。然后只服务器执行循环

asc_center_pos = getArray(configFile >> "CfgWorlds" >> worldName >> "centerPosition");

if (A3E_Debug) then {
	asc_dis_blood = 0; //血迹距离
	asc_dis_easy = 0;  //简单对象清理
	asc_dis_paricle = 0; //暂时没用
	asc_dis_live_man = 1200; //杀死所有清理单位（在玩家一定范围外）1200
	asc_dis_veh = 500; //清除载具距离
	asc_dis_dead = 0;  //死亡单位
	asc_dis_mine = 2000; //地雷
} else {
	asc_dis_blood = 80;
	asc_dis_easy = 80;
	asc_dis_paricle = 80;
	asc_dis_live_man = 1200;
	asc_dis_veh = 1000;
	asc_dis_dead = 80;
	asc_dis_mine = 2000;
};

// -------------------------------------特定对象清理-------------------------------------
// 清理血迹等特定veh（不一定是血迹）
asc_blood = [
	"BloodSplatter_01_Large_New_F",
	"BloodSplatter_01_Medium_New_F",
	"BloodSplatter_01_Small_New_F",
	"BloodSpray_01_New_F",
	"BloodPool_01_Large_New_F",
	"BloodPool_01_Medium_New_F",
	"BloodTrail_01_New_F",
	"UserTexture1m_F",
	"UserTexture_1x2_F"
];

fn_cleanup_blood = {
	private _num = 0;
	_allPlayers = allUnits select {
		isPlayer _x && {
			!(_x isKindOf "HeadlessClient_F")
		}
	};

	_BloodSplatter = nearestObjects [asc_center_pos, asc_blood, worldSize];

	{
		_Delete_this = _x;
		if ((({
			((_x distance _Delete_this) <= asc_dis_blood)
		} count _allPlayers) isEqualTo 0)) then
		{
			_num = _num + 1;
			deleteVehicle _x
		};
	} forEach _BloodSplatter;
	_num
};

// -------------------------------------简单对象清理-------------------------------------
asc_easy = [
	"BloodSplatter_01_Large_New_F",
	"BloodSplatter_01_Medium_New_F",
	"BloodSplatter_01_Small_New_F",
	"BloodSpray_01_New_F",
	"BloodPool_01_Large_New_F",
	"BloodPool_01_Medium_New_F",
	"BloodTrail_01_New_F",
	"UserTexture1m_F",
	"UserTexture_1x2_F"
];

fn_cleanup_easy = {
	private _num = 0;
	_allPlayers = allUnits select {
		isPlayer _x && {
			!(_x isKindOf "HeadlessClient_F")
		}
	};

	_BloodSplatter = allSimpleObjects asc_easy;

	{
		_Delete_this = _x;
		if ((({
			((_x distance _Delete_this) <= asc_dis_easy)
		} count _allPlayers) isEqualTo 0)) then
		{
			_num = _num + 1;
			deleteVehicle _x
		};
	} forEach _BloodSplatter;
	_num
};

// 本地
fn_cleanup_local = {
	private _num = 0;
	{
		deleteVehicle _x;
		_num = _num + 1
	} forEach (allMissionObjects "#slop");
	{
		deleteVehicle _x;
		_num = _num + 1
	} forEach (allMissionObjects "#mark");
	{
		deleteVehicle _x;
		_num = _num + 1
	} forEach (allMissionObjects "#track");
	{
		deleteVehicle _x;
		_num = _num + 1
	} forEach (allMissionObjects "#crater");
	{
		deleteVehicle _x;
		_num = _num + 1
	} forEach (allMissionObjects "#crateronvehicle");
	{
		deleteVehicle _x;
		_num = _num + 1
	} forEach (allMissionObjects "#explosion");
	{
		deleteVehicle _x;
		_num = _num + 1
	} forEach (allMissionObjects "#objectdestructed");
	{
		deleteVehicle _x;
		_num = _num + 1
	} forEach (allMissionObjects "#soundonvehicle");
	{
		deleteVehicle _x;
		_num = _num + 1
	} forEach (allMissionObjects "#dynamicsound");
	{
		deleteVehicle _x;
		_num = _num + 1
	} forEach (allMissionObjects "#particlesource");

	_num = _num + (call fn_cleanup_blood);
	_num = _num + (call fn_cleanup_easy);

	hint format["本地清理了: %1物体", _num];
};

// 服务器
fn_cleanup_server =
{
	private _num_dead = 0;
	private _num_veh = 0;
	private _num_mine = 0;
	private _num_item = 0;
	private _num_other = 0;
	private _num_group = 0;
	private _num_trigger = 0;

	// 杀死所有清理单位（在玩家一定范围外）1200
	{
		if ((!isNull _x) && (alive _x) && (!isPlayer _x) && (!((group _x) getVariable["no_cleanup", false]))) then {
			private _unit = _x;
			private _clean = true;
			{
				if (_x distance _unit < asc_dis_live_man) exitWith {
					_clean = false;
				};
			}forEach allPlayers;
			if (_clean) then {
				deleteVehicle _unit;// setDamage [1, false];
			};
		};
	}forEach allUnits;

	// 移除玩家范围外所有载具，标记载具不清理 1000m
	// {
	// 	private _veh = _x;
	// 	if !(_veh getVariable["no_cleanup", false]) then
	// 	{
	// 		private _clean = true;
	// 		{
	// 			if(_x distance _veh < asc_dis_veh) exitWith
	// 			{
	// 				_clean = false;
	// 			};
	// 		}forEach AllPlayers;
	// 		if(_clean) then {
	// 			{
	// 				deleteVehicle _x;//setDamage [1, false];
	// 			} forEach (crew _veh);
	// 			deleteVehicle _veh;
	// 			_num_veh = _num_veh + 1;
	// 		};
	// 	}
	// } forEach ((allMissionObjects "Car") + (allMissionObjects "Tank") + (allMissionObjects "Ship") + (allMissionObjects "Air"));

	// 消除死亡单位 80米以外
	{
		if ((!isPlayer _x)) then {
			private _veh = _x;
			private _clean = true;
			{
				if (_x distance _veh < asc_dis_dead) exitWith {
					_clean = false;
				};
			}forEach allPlayers;
			if (_clean || _x getVariable["check_dead_count", 0] == 1) then {
				deleteVehicle _veh;
				_num_dead = _num_dead + 1;
			} else {
				_x setVariable["check_dead_count", 1];
			};
		};
	}forEach allDead;

	// 移除地面物品
	{
		private _veh = _x;
		private _clean = true;
		{
			if (_x distance _veh < asc_dis_dead) exitWith {
				_clean = false;
			};
		}forEach allPlayers;
		if (_clean) then {
			deleteVehicle _veh;
			_num_item = _num_item + 1;
		};
	} forEach (allMissionObjects "WeaponHolder");

	// 移除地雷
	{
		private _veh = _x;
		private _clean = true;
		{
			if (_x distance _veh < asc_dis_mine) exitWith {
				_clean = false;
			};
		}forEach allPlayers;
		if (_clean) then {
			deleteVehicle _veh;
			_num_mine = _num_mine + 1;
		};
	} forEach allMines;

	// 移除弹坑
	{
		deleteVehicle _x;
		_num_other = _num_other + 1
	} forEach ((allMissionObjects "Crater") + (allMissionObjects "CraterLong") + (allMissionObjects "CraterLong_small"));

	// 移除废墟
	{
		deleteVehicle _x;
		_num_other = _num_other + 1
	} forEach (allMissionObjects "ruins");

	// 空组 第一次循环不删除，再次循环还是空的话将它删除。
	// {
	// 	if ( ((count units _x) isEqualTo 0) && _x getVariable["check_count", 0] == 1 ) then
	// 	{
	// 		deleteGroup _x;
	// 		_num_group = _num_group + 1
	// 	} else {
	// 		 _x setVariable["check_count", 1];
	// 	};
	// } count allGroups;

	// 触发器
	// {
	// 	deleteVehicle _x;
	// 	_num_trigger = _num_trigger + 1
	// } forEach (allMissionObjects "emptyDetector");

	format["服务器清理了: %1尸体 %2载具 %3地面物品 %4较远的地雷 %5特殊效果 %6空单位组 %7脚本触发器", _num_dead, _num_veh, _num_item, _num_mine, _num_other, _num_group, _num_trigger] remoteExec ["hint"];
};

fn_cleanup_all =
{
	// 先清理本地
	[] remoteExec ["fn_cleanup_local", 0];

	uiSleep 20;

	[] call fn_cleanup_server;
};

if (isServer) then {
	while { true } do {
		uiSleep 300;
		"即将开始执行服务器清理操作" remoteExec ["hint"];
		uiSleep 10;
		[] spawn fn_cleanup_all;
	};
};